package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckGroupDetailDao;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 */
@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckItemDao  checkItemDao;
    @Autowired
    private CheckGroupDetailDao  checkGroupDetailDao;


    /**
     * 检查组分页
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
      Page<CheckGroup>  page =   checkGroupDao.pageQuery(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 检查组新建
     * @param checkGroup
     */

    @Override
    public void add(CheckGroup checkGroup,Integer[] ids) {
        checkGroupDao.insert(checkGroup);
        Integer checkGroupId = checkGroup.getId();

        for (Integer checkItemId : ids) {
            checkGroupDetailDao.insert(checkGroupId,checkItemId);
        }

    }

    /**
     * 查询检查组表进行数据回显
     * @param id
     * @return
     */
    @Override
    public List<Integer> selectById(Integer id) {
        return checkGroupDao.selectById(id);
    }

    /**
     * 查询中间表的id进行回显
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(Integer id) {
        return checkItemDao.findById(id);
    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    public void update(CheckGroup checkGroup,Integer[] checkitemIds) {
       //清除原有数据
        checkGroupDetailDao.delete(checkGroup.getId());
        setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
        checkGroupDao.update(checkGroup);

    }

    /**
     * 删除检查组
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        checkGroupDetailDao.delete(id);
        checkGroupDao.deletByid(id);

    }

    /**
     * 查询管理组所有信息
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {
      List<CheckGroup>  checkGroupList =  checkGroupDao.findAll();
        return checkGroupList;
    }

    /**
     * 设置中间表数据
     * @param checkGroupId
     * @param checkItemIds
     */
    public  void   setCheckGroupAndCheckItem(Integer  checkGroupId,Integer[] checkItemIds){
        if (checkItemIds !=null && checkItemIds.length>0){
            for (Integer checkItemId : checkItemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkGroupId",checkGroupId);
                map.put("checkItemId",checkItemId);
                checkGroupDao.insertCheckGroupAndCheckItem(map);

            }
        }
    }


}
